# -*- coding: utf-8 -*-

from __future__ import with_statement
from flask import Flask, request, session, g, redirect, url_for, abort, \
     render_template, flash, _app_ctx_stack
from flask.ext.cache import Cache
import stations_list, get_weather, get_news, get_tweets, play_radio

# configuration
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'a'
PASSWORD = 'a'

app = Flask(__name__)
cache = Cache(app)
app.config.from_object(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)


@app.route('/', methods=['GET','POST'])
@cache.cached(timeout=50)
def show_main_menu():
    options = ['Radio','Pandora','Weather','News']
    tweets = get_tweets.get_tweets()
    return render_template('main.html',options=options,tweets=tweets)

@app.route('/Weather', methods=['GET','POST'])
@cache.cached(timeout=50)
def weather():
    weather_cond = get_weather.get_weather()
    return render_template('weather.html',weather_cond=weather_cond)
   
@app.route('/Radio', methods=['GET','POST'])
@cache.cached(timeout=50)
def radio():
    stations,categories = stations_list.station_list()
    options = stations
    return render_template('submenu.html',options=options)

@app.route('/Stations', methods=['GET','POST'])
@cache.cached(timeout=50)
def stations():
    category = request.args.get('category')
    stations,categories = stations_list.station_list()
    options = stations[category]
    return render_template('stations.html',category=category,options=options)

@app.route('/play_station', methods=['GET','POST'])
@cache.cached(timeout=50)
def play_station():
    link = request.args.get('link')
    category = request.args.get('category')
    play_radio.play(link)
    return redirect(url_for('stations',category=category))    

@app.route('/News', methods=['GET','POST'])
@cache.cached(timeout=50)
def news():
    news_titles = get_news.get_news("world")
    return render_template('news.html',news_titles=news_titles)

@app.route('/Pandora', methods=['GET','POST'])
@cache.cached(timeout=50)
def pandora():
    weather_cond = get_weather.get_weather()
    return render_template('weather.html',weather_cond=weather_cond)

if __name__ == '__main__':
    app.run()
    #app.run( app.run(host='0.0.0.0', port=8000))#This defaults to port 8000
